package com.zch.jh.web.rest;

import com.zch.jh.service.ReceiveAddrService;
import com.zch.jh.web.rest.errors.BadRequestAlertException;
import com.zch.jh.service.dto.ReceiveAddrDTO;

import io.github.jhipster.web.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing {@link com.zch.jh.domain.ReceiveAddr}.
 */
@RestController
@RequestMapping("/api")
public class ReceiveAddrResource {

    private final Logger log = LoggerFactory.getLogger(ReceiveAddrResource.class);

    private static final String ENTITY_NAME = "receiveAddr";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final ReceiveAddrService receiveAddrService;

    public ReceiveAddrResource(ReceiveAddrService receiveAddrService) {
        this.receiveAddrService = receiveAddrService;
    }

    /**
     * {@code POST  /receive-addrs} : Create a new receiveAddr.
     *
     * @param receiveAddrDTO the receiveAddrDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new receiveAddrDTO, or with status {@code 400 (Bad Request)} if the receiveAddr has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/receive-addrs")
    public ResponseEntity<ReceiveAddrDTO> createReceiveAddr(@RequestBody ReceiveAddrDTO receiveAddrDTO) throws URISyntaxException {
        log.debug("REST request to save ReceiveAddr : {}", receiveAddrDTO);
        if (receiveAddrDTO.getId() != null) {
            throw new BadRequestAlertException("A new receiveAddr cannot already have an ID", ENTITY_NAME, "idexists");
        }
        ReceiveAddrDTO result = receiveAddrService.save(receiveAddrDTO);
        return ResponseEntity.created(new URI("/api/receive-addrs/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /receive-addrs} : Updates an existing receiveAddr.
     *
     * @param receiveAddrDTO the receiveAddrDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated receiveAddrDTO,
     * or with status {@code 400 (Bad Request)} if the receiveAddrDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the receiveAddrDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/receive-addrs")
    public ResponseEntity<ReceiveAddrDTO> updateReceiveAddr(@RequestBody ReceiveAddrDTO receiveAddrDTO) throws URISyntaxException {
        log.debug("REST request to update ReceiveAddr : {}", receiveAddrDTO);
        if (receiveAddrDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        ReceiveAddrDTO result = receiveAddrService.save(receiveAddrDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, receiveAddrDTO.getId().toString()))
            .body(result);
    }

    /**
     * {@code GET  /receive-addrs} : get all the receiveAddrs.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of receiveAddrs in body.
     */
    @GetMapping("/receive-addrs")
    public List<ReceiveAddrDTO> getAllReceiveAddrs() {
        log.debug("REST request to get all ReceiveAddrs");
        return receiveAddrService.findAll();
    }

    /**
     * {@code GET  /receive-addrs/:id} : get the "id" receiveAddr.
     *
     * @param id the id of the receiveAddrDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the receiveAddrDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/receive-addrs/{id}")
    public ResponseEntity<ReceiveAddrDTO> getReceiveAddr(@PathVariable Long id) {
        log.debug("REST request to get ReceiveAddr : {}", id);
        Optional<ReceiveAddrDTO> receiveAddrDTO = receiveAddrService.findOne(id);
        return ResponseUtil.wrapOrNotFound(receiveAddrDTO);
    }

    /**
     * {@code DELETE  /receive-addrs/:id} : delete the "id" receiveAddr.
     *
     * @param id the id of the receiveAddrDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/receive-addrs/{id}")
    public ResponseEntity<Void> deleteReceiveAddr(@PathVariable Long id) {
        log.debug("REST request to delete ReceiveAddr : {}", id);
        receiveAddrService.delete(id);
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString())).build();
    }
}
